<%@ page import="java.io.File" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.FileReader" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>体重计算器</title>
    <style type="text/css">
        input[type=text] {
            width: 100%;
            color: blue;
            font-weight: bold;
            font-size: xx-large;
            font-family: "Courier New";
        }

        div {
            width: 260px;
            color: green;
        }
    </style>
</head>
<body>

<%
    String sex = request.getParameter("sex");
    double realWeight = Double.parseDouble(request.getParameter("weight"));
    double realHeight = Double.parseDouble(request.getParameter("height"));

    double standWeight = "M".equals(sex) ? (realHeight - 105) * 0.95 : ((realHeight - 105) * 0.95 - 2.5);
    double rate = (realWeight - standWeight) / standWeight;
    String msg = "我都心动了，您的身材属于标准身材，继续保持！";
    String img = "完美";
    String fileName = "";
    if (Math.abs(rate) <= 0.05) {
        msg = "我都心动了，您的身材属于标准身材，继续保持！";
        img = "完美.png";
    } else {
        if (rate > 0) {
            fileName = "fat.txt";
            img = "fat.gif";
        } else {
            fileName = "thin.txt";
            img = "thin.gif";
        }
        List<String> info = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(new FileReader(new File(request.getRealPath("/") + "request/weight/image/" + fileName)));
        String line = null;
        while ((line = reader.readLine()) != null) {
            info.add(line);
        }
        int num = (int) (Math.random() * info.size());
        msg = info.get(num);
    }


%>

<center>
    <H1>体重计算器</H1>
    <form action="cal.jsp">
        <table border="1px" width="360px">
            <tr>
                <td>
                    您的性别为：
                </td>
                <td>
                    <img src="image/<%="F".equals(sex)?"女":"男"%>.png" width="64" height="64">
                </td>
            </tr>
            <tr>
                <td>
                    您的身高为：
                </td>
                <td>
                    <%=realHeight%>CM
                </td>
            </tr>
            <tr>
                <td>
                    您的体重为：
                </td>
                <td>
                    <%=realWeight%>KG
                </td>
            </tr>

            <tr>
                <td>
                    您的标准体重为：
                </td>
                <td>
                    <%=standWeight%>KG
                </td>
            </tr>

            <tr>
                <td>
                    您的体重系数：
                </td>
                <td>
                    <%=rate%>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center" style="color: red">
                   <h3> <%=msg%></h3>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <img src="image/<%=img%>" width="64" height="64">
                </td>
            </tr>
        </table>
    </form>

</center>

</body>
</html>
